from flask import Blueprint,request,jsonify
from db import db1
bp_article = Blueprint('article',__name__)


@bp_article.route('/articleM')
def articleM():
    #获取id
    id = request.args.get('id')
    #根据id查询
    sql = "select * from article where id=%d"%(int(id))
    res = db1.find(sql)
    #返回
    return jsonify({'code':200,'article':res})


from auser import get_data
@bp_article.route('/commentM',methods=['POST','GET'])
def commentM():
    if request.method == 'POST':
        #获取客户提交的信息
        data = get_data()
        #构造sql语句
        sql = "insert into ccomment values(0,%d,%d,'%s')"%(data['userid'],data['aid'],data['content'])
        #添加
        db1.insert(sql)
        #返回
        return jsonify({'code':200})
    else:
        #获取aid
        aid = request.args.get('aid')
        #根据文章id获取评价内容，用户名
        #构造sql
        sql = "select c.content,u.name from ccomment as c inner join cuser as u where c.aid=%d"%(int(aid))
        res = db1.findall(sql)
        #返回
        return jsonify({'code':200,'list':res})

